using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Demo.Calendar.Code;
using System.Collections;
namespace Demo.Calendar.Features.AddCalendarArtifacts
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("4c1beeb2-9bb5-488f-8220-53e2627b6ebe")]
    public class AddCalendarArtifactsEventReceiver : SPFeatureReceiver
    {
    

        const string columnGroup = "Demo";

        const string ctName = "RollUpCalendarContentType";

        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPWeb spWeb = properties.GetWeb() as SPWeb)
            {



                //add the fields
                addFields(spWeb);






                //add content type
                SPContentType testCT = spWeb.ContentTypes[ctName];
                // we will not create the content type if it exists 
                if (testCT == null)
                {
                    //the content type does not exist add it
                    addContentType(spWeb, ctName);

                }



            }

        }

        public void addFields(SPWeb spWeb)
        {





            Utilities.addLookupField(spWeb, "CalendarCategories", "Title", "MilCategory", columnGroup);

        }
        //need to add lookup list prior to creating the content type
        private static void addContentType(SPWeb spWeb, string name)
        {
            SPContentType myContentType = new SPContentType(spWeb.ContentTypes["Event"], spWeb.ContentTypes, name) { Group = columnGroup };

            spWeb.ContentTypes.Add(myContentType);
            addContentTypeLinkages(spWeb, myContentType);
            myContentType.Fields.Delete("Category");


            myContentType.Update();
        }


        public static void addContentTypeLinkages(SPWeb spWeb, SPContentType ct)
        {

            Utilities.addContentTypeLink(spWeb, "MilCategory", ct);


        }

   

    }
}
